﻿using System;
using System.Collections.Generic;
using HKEA.CMS.Core.BLL;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using iMaker.Library;
using HKEA.CMS.Core.BLL.Tbl;

namespace WebApplication
{
    public partial class WebLogin :BasePage
    {
        protected  string CustomerUID = string.Empty;
        protected  string CustomerID = string.Empty;
        private int defaultCurPage = 1;
        private string defaultPropertyName = "CreationDate";
        private string defaultOrderBy = "desc";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CustomerUID = Request.QueryString[Constant.UrlParamName.CustomerUID];
                string page = Request.QueryString[Constant.UrlParamName.CurrentPage];
                string propertyname = Request.QueryString[Constant.UrlParamName.PropertyName];
                string orderby = Request.QueryString[Constant.UrlParamName.OrderBy];

                if (string.IsNullOrEmpty(CustomerUID))
                {
                    usermeg.Visible = false;
                }
                else
                {
                    tblCustomer entCust=GetCustomer(int.Parse(CustomerUID));
                    CustomerID = entCust != null ? entCust.CustomerID : string.Empty;
                    if (!string.IsNullOrEmpty(propertyname) && !string.IsNullOrEmpty(orderby))
                    {
                        defaultPropertyName = propertyname; defaultOrderBy = orderby;
                    }

                    if (!string.IsNullOrEmpty(page))
                    {
                        defaultCurPage = int.Parse(page);
                    }

                    BindDataSource(defaultCurPage, defaultPropertyName, defaultOrderBy);

                    CreateSendEmailBox();
                }
            }
        }

        /// <summary>
        /// 根据参数（当前页和排序 字段和升降序） 绑定 列表
        /// </summary>
        /// <param name="curPage"></param>
        /// <param name="strPropertyName"></param>
        /// <param name="strOrderBy"></param>
        private void BindDataSource(int curPage, string strPropertyName, string strOrderBy)
        {
            tblCust_WebLoginBLL bll = new tblCust_WebLoginBLL();
            List<tblCust_WebLogin> list = new List<tblCust_WebLogin>();
            tblCust_WebLogin entParam = new tblCust_WebLogin();
            int custUID = 0;
            int.TryParse(CustomerUID, out custUID);
            //by jimmy
            entParam.CustomerUID = custUID;
            // end
            PagerCollection pager = new PagerCollection();
            pager.PageSize = Constant.PerPageRecordCount.common;
            pager.CurPage = curPage;

            paginator1.pager = pager;
            paginator2.pager = pager;

            list = bll.GetListByENT(entParam, pager, strPropertyName, strOrderBy);

            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }

        public void CreateSendEmailBox()
        {
            string ret = "<tr><td>暂时没有任何授权联系人</td></tr>"; 
            string custuid = CustomerUID;
            if (!string.IsNullOrEmpty(custuid))
            {
                List<ContactPersonListDetail> list_cpld = new List<ContactPersonListDetail>();
                list_cpld = tblCust_ContactPersonBLL.GetInstance().GetCPListDetail(int.Parse(custuid));

                for (int i = 0; i < list_cpld.Count; i++)
                {
                    tblCPerson_Attribute entCPA = new tblCPerson_Attribute();
                    entCPA.AttributeUID = 10;
                    entCPA.ContactPersonUID = list_cpld[i].CPUID;
                    if (!tblCPerson_AttributeBLL.GetInstance().IsExist(entCPA))
                        list_cpld.RemoveAt(i);
                }

                int listCount = list_cpld.Count;
                if (listCount > 0)
                {
                    ret = string.Empty;
                    for (int i = 0; i < listCount; i++)
                    {
                        if (i == 3)
                        {
                            var temp = list_cpld[i].ContactInfo;
                        }
                        string[] list_email = { list_cpld[i].ContactInfo.Email1, list_cpld[i].ContactInfo.Email2, list_cpld[i].ContactInfo.Email3, list_cpld[i].ContactInfo.Email4, list_cpld[i].ContactInfo.Email5 };
                        foreach (string e in list_email)
                        {
                            //if (!string.IsNullOrEmpty(e.Trim())) edit by martin
                            if (!string.IsNullOrEmpty(e) && !string.IsNullOrEmpty(e.Trim()))
                            {
                                ret += string.Format("<tr><td><input id='checkbox_{0}' type='checkbox' value='{1}'><label for=''>{2}</label></td></tr>", list_cpld[i].CPUID, e.Trim(), list_cpld[i].FullName + " - " + e.Trim());
                            }
                        }
                    }            
                }
            }
            ltlSendEmailBox.Text = ret;
        }
        protected void WriteToLog()
        {
            tblUser ent = GetManageFromSession();
            tblHistory entlog = new tblHistory();
            entlog.HistoryCatUID = 1;
            entlog.HistoryTypeUID = 2;
            entlog.CustomerUID = Convert.ToInt32(CustomerUID);
            entlog.Cust_MemberUID = 0;
            entlog.ContactPersonUID = 0;
            entlog.History_Detail = ent.User_Name + " {WebLogin}have add a Account";
            entlog.LastModifyBy = ent.User_Name;
            int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
        }
        #region 辅助方法
        /// <summary>
        /// 获取状态字符串
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        protected string GetStatusString(object val)
        {
            string ret = string.Empty;
            if (val != null)
            {
                if (val.Equals(Constant.WebLoginStatus.Active))
                    ret = "Active";
                else
                    ret = "Inactive";
            }
            return ret;
        }

        protected tblCustomer GetCustomer(int uid)
        {
            tblCustomerBLL bllCust = new tblCustomerBLL();
            return bllCust.GetEntity(uid);
        }

        #endregion
    }
}